#!/usr/bin/python
#-*-coding:utf-8-*-
import sys,getopt
from main import myAlgorithm
import datetime

if __name__ == '__main__':
    opts,args = getopt.getopt(sys.argv[1:],"hi:o:",["version","file=","pk=","sk=","rk=","me="])
    input_file = ''         #the input file
    output_file = ''        #the output file
    keyinit = [2,2,2]         # the key
    method = 'en'
    for op, value in opts:
        #print op,value
        if op == '-i':
            input_file = value
        elif op == '--me':
            method = value.strip()
        elif op == '-o':
            output_file = value
        elif op == '-h':
            print 'A instance:'
            print '\t python app.py -i /home/aaa/i.jpg -o /home/aaa/o.jpg -me=en --sk=2 --pk=3 --rk=2'
            print 'Options:'
            print '\t-i the absolute path of the input file.'
            print '\t-o the absolute path of the output file.'
            print '\t--me the which function should be used.'
            print '\t\t"en" refer to encrypt.\n\t\t"de" refer to decrypt.'
            print '\t--pk= the Pertutation key.'
            print '\t--sk= the Substitution key.'
            print '\t--rk= the Width key.'
            print '\t-h the help information.'
            sys.exit()
        elif op == '--pk':
            keyinit[0] = float(value)
        elif op == '--sk':
            keyinit[1] = float(value)
        elif op == '--rk':
            keyinit[2] = float(value)
        else:
            print '\tOption error!'
            sys.exit()
    if input_file and output_file:
        myal = myAlgorithm()
        if method == 'en':
            p1 = datetime.datetime.now()
            myal.EncryptFile(input_file,keyinit,False,output_file)
            p2 = datetime.datetime.now()
            #print 'time:' + (p2 - p1).strftime('%Y-%m-%d %H:%M:%S.%f')
            #print 'time:' + str((p2 - p1).total_seconds())
        elif method == 'de':
            myal.DecryptFile(input_file,keyinit,False,output_file)
        else:
            print '\tPlease check option!'
            sys.exit()
    else:
        print '\tPlease check option!'
        sys.exit()


